package com.jxtti.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;

import net.sf.json.JSONObject;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jxtti.entity.Admin_RoleType;
import com.jxtti.entity.Admin_UserRole;
import com.jxtti.model.User;
import com.jxtti.service.AdminRoleTypeService;
import com.jxtti.service.AdminUserRoleService;
import com.jxtti.service.DistrictService;
import com.jxtti.service.UserService;
import com.jxtti.utils.AuthUtil;
import com.jxtti.utils.Constants;
import com.jxtti.utils.SqlSessionCache;

@Controller
public class IndexController {

	@Autowired
	private UserService userService;

/*	@Autowired
	private MenuService menuService;*/

	@Autowired
	private SqlSessionCache ssc;
	
	@Autowired 
	private AdminUserRoleService  adminUserRoleService; 
	
	@Autowired 
	private AdminRoleTypeService  adminRoleTypeService; 
	
	@Autowired
	private DistrictService districtService;
	
	

	@RequestMapping(value = "/refreshMapper")
	public void refreshMapper(@RequestParam Map<String, String> data,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		boolean ret = ssc.refreshMapper();
		if (ret) {
			response.getWriter().print("MyBatis映射文件缓存刷新成功！");
		} else {
			response.getWriter().print("MyBatis映射文件缓存刷新失败！");
		}
	}

	@RequestMapping(value = "/wstest")
	public void wstest(@RequestParam Map<String, String> data,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		try {
			JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory
					.newInstance();
			Client client = dcf
					.createClient("http://111.75.227.203:9180/eipp-webservice/services/queryInfo?wsdl");
			// url为调用webService的wsdl地址
			QName name = new QName("http://ws.jxtti.com/", "getCityInfo");
			// namespace是命名空间，methodName是方法名
			Object[] objects = client.invoke(name, "1000", "AF934AC035");
			if (null != objects) {
				JSONObject jsonObject = JSONObject.fromObject(objects[0]);
				response.getWriter().print(jsonObject.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 单点登录接口
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/sso")
	public void sso(@RequestParam Map<String, String> data,
			HttpServletRequest request, HttpServletResponse response) {
//			throws IOException {
//		response.setContentType("text/html;charset=UTF-8");
//		try {
//			String token = data.get("token");
//			long timestamp = Long.parseLong(data.get("timestamp"));
//			long currentTime = System.currentTimeMillis() / 1000;
//			long diff = currentTime - timestamp;
//			if (Math.abs(diff) > 60 * 15) {
//				// 时差超过15分钟
//				response.getWriter().print("链接已失效！");
//			} else {
//				String username = "";
//				JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory
//						.newInstance();
//				// url为调用webService的wsdl地址
//				Client client = dcf.createClient(Constants.WSDL_URL);
//				// namespace是命名空间，methodName是方法名
//				QName name = new QName(Constants.NAMESPACE_URI,
//						"getUserCodeFromToken");
//				Object[] objects = client.invoke(name, token);
//				if (null != objects) {
//					JSONObject jsonObject = JSONObject.fromObject(objects[0]);
//					if (null == jsonObject.getString("userCode")) {
//						response.getWriter().print("链接已失效！");
//					} else {
//						username = jsonObject.getString("userCode");
//					}
//					if (!username.isEmpty()) {
//						User user = userService.findUserByName(username);
//						// 将功能权限放到session中
//						data.put("roleId", user.getRoleId().toString());
//						/*List<Map<String, Object>> menus = this.menuService.getMenuAction(data);
//						request.getSession().setAttribute("menus", menus);*/
//
//						Map<String, Object> dataWeb = new HashMap<String,Object>();	
//						dataWeb.put("fk_UserId", user.getId());			
//						dataWeb.put("is_deleted",0);	
//						dataWeb.put("status",1);	
//						
//						
//						Map<String, String> gzdata =new HashMap<String, String>();	
//						gzdata.put("id",user.getOrgId().toString());			
//						//DbContextHolder.setDbType(DataSourceConst.SLAVE); //调用service之前只需要设置上下文 调用相应的数据源						
//							String orgName=districtService.getFullNameById(gzdata);			
//						//DbContextHolder.clearDbType();			
//						user.setOrgName(orgName);		
//						
//						List<Admin_UserRole> userRoleWeb=adminUserRoleService.queryAll(dataWeb);
//						if(userRoleWeb.size()==0||userRoleWeb==null){
//							user.setRoleId(null);
//							user.setRoleName(null);
//						}else{
//							user.setRoleId(userRoleWeb.get(0).getFk_RoleType());
//							user.setRoleName(userRoleWeb.get(0).getRoleName());
//							//查看用户是否管理员
//							Admin_RoleType roleType=new Admin_RoleType();
//							roleType.setId(userRoleWeb.get(0).getFk_RoleType());
//							roleType=adminRoleTypeService.queryListById(roleType.getId());
//							user.setDistrictId(String.valueOf(roleType.getIsAdmin()));
//						}			
//						AuthUtil.setUser(request, user);
//						
//						
//						
//						
//						AuthUtil.setUser(request, user);
//						response.sendRedirect(Constants.SYSTEM_PATH
//								+ "AdminUser/mainInterface.htm");
//						
//
//					} else {
//						throw new Exception("web service服务调用失败");
//					}
//				} else {
//					throw new Exception("web service服务调用失败");
//				}
//			}
//		} catch (Exception e) {
//			response.getWriter().print("登录服务异常或者链接无效！");
//			e.printStackTrace();
//		}
//
//	}
	}
}
